package com.yuncheng.spcyApi.spcy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.entity.SysZyzgkyqk;
import com.yuncheng.spcyApi.mapper.SysZyzgkyqkMapper;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;

/**
 * @author hua
 */
@Api(tags = "用户-专业技术资质及参与科研项目及研究药品情况")
@RestController
@RequestMapping("/api/spzx-spcy/zyzgkq")
public class SysZyzgkyqkController {

    @Resource
    private SysZyzgkyqkMapper sysZyzgkyqkMapper;

    @Log(title = "专业技术资质-列表")
    @ApiOperation(value = "专业技术资质-列表")
    @GetMapping(value = "getZyjszzList")
    public HttpResult getZyjszzList(@RequestParam(value = "fuserid",defaultValue = "") String fuserid){

        if (StringUtils.isBlank(fuserid)){
            fuserid = GetCurrentUserUtils.getCurrentUserId();
        }
        return HttpResultVo.HttpResultList(sysZyzgkyqkMapper.selectList(new LambdaQueryWrapper<SysZyzgkyqk>()
                .eq(SysZyzgkyqk::getFsjy, "0")
                .eq(SysZyzgkyqk::getFuserid, fuserid)));
    }

    @Log(title = "科研项目-列表")
    @ApiOperation(value = "科研项目-列表")
    @GetMapping(value = "getKyxmList")
    public HttpResult getKyxmList(@RequestParam(value = "fuserid",defaultValue = "") String fuserid){

        if (StringUtils.isBlank(fuserid)){
            fuserid = GetCurrentUserUtils.getCurrentUserId();
        }
        return HttpResultVo.HttpResultList(sysZyzgkyqkMapper.selectList(new LambdaQueryWrapper<SysZyzgkyqk>()
                .eq(SysZyzgkyqk::getFsjy, "1")
                .eq(SysZyzgkyqk::getFuserid, fuserid)));
    }

    @Log(title = "研究药品情况-列表")
    @ApiOperation(value = "研究药品情况-列表")
    @GetMapping(value = "getYjypqkList")
    public HttpResult getYjypqkList(@RequestParam(value = "fuserid",defaultValue = "") String fuserid){

        if (StringUtils.isBlank(fuserid)){
            fuserid = GetCurrentUserUtils.getCurrentUserId();
        }

        return HttpResultVo.HttpResultList(sysZyzgkyqkMapper.selectList(new LambdaQueryWrapper<SysZyzgkyqk>()
                .eq(SysZyzgkyqk::getFsjy, "2")
                .eq(SysZyzgkyqk::getFuserid, fuserid)));
    }

    @Log(title = "专业技术资质-添加")
    @ApiOperation(value = "专业技术资质-添加")
    @PostMapping(value = "addZyjszz")
    public HttpResult addZyjszz(@RequestBody SysZyzgkyqk bean){
        if (StringUtils.isBlank(bean.getFuserid())){
            bean.setFuserid(GetCurrentUserUtils.getCurrentUserId());
        }

        bean.setId(GetUuIdUtils.ReplaceUuId());
        bean.setFsjy("0");
        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        sysZyzgkyqkMapper.insert(bean);
        return HttpResultVo.HttpResultSave(bean, "添加成功");
    }

    @Log(title = "科研项目-添加")
    @ApiOperation(value = "科研项目-添加")
    @PostMapping(value = "addKyxm")
    public HttpResult addKyxm(@RequestBody SysZyzgkyqk bean){
        if (StringUtils.isBlank(bean.getFuserid())){
            bean.setFuserid(GetCurrentUserUtils.getCurrentUserId());
        }

        bean.setId(GetUuIdUtils.ReplaceUuId());
        bean.setFsjy("1");
        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        sysZyzgkyqkMapper.insert(bean);
        return HttpResultVo.HttpResultSave(bean, "添加成功");
    }

    @Log(title = "研究药品情况-添加")
    @ApiOperation(value = "研究药品情况-添加")
    @PostMapping(value = "addYjypqk")
    public HttpResult addYjypqk(@RequestBody SysZyzgkyqk bean){
        if (StringUtils.isBlank(bean.getFuserid())){
            bean.setFuserid(GetCurrentUserUtils.getCurrentUserId());
        }

        bean.setId(GetUuIdUtils.ReplaceUuId());
        bean.setFsjy("2");
        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        sysZyzgkyqkMapper.insert(bean);
        return HttpResultVo.HttpResultSave(bean, "添加成功");
    }

    @Log(title = "专业技术资质/科研项目/研究药品情况-更新")
    @ApiOperation(value = "专业技术资质/科研项目/研究药品情况-更新")
    @PostMapping(value = "/updateZyzgky")
    public HttpResult<?> updateZyzgky(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("参数为空！");
        }
        String dataId = buffer.get("id");
        SysZyzgkyqk updateBean = sysZyzgkyqkMapper.selectById(dataId);
        if (updateBean == null){
            return HttpResult.error("没有想匹配的数据！");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);
        sysZyzgkyqkMapper.updateById(updateBean);

        return HttpResultVo.HttpResultSave(updateBean, "保存成功");
    }

    @Log(title = "专业技术资质/科研项目/研究药品情况-批量删除")
    @ApiOperation(value = "专业技术资质/科研项目/研究药品情况-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam(value = "ids",defaultValue = "") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("参数为空！");
        }
        sysZyzgkyqkMapper.deleteBatchIds(StringPatternUtils.getListByIds(ids));

        return HttpResult.ok("删除成功");
    }

    @Log(title = "专业技术资质/科研项目/研究药品情况-通过id查询")
    @ApiOperation(value = "专业技术资质/科研项目/研究药品情况-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam(value = "id",defaultValue = "") String id) {

        return HttpResult.ok(sysZyzgkyqkMapper.selectById(id));
    }
}
